//----------------------------------------------------------------------------
//  (c) 2007    Sergio Lora Garcia
//----------------------------------------------------------------------------
#include "gui/nguiwindowslider.h"
#include "gui/nguiserver.h"
#include "gui/nguiskin.h"
#include "kernel/ntimeserver.h"
#include "gui/nguiwidget.h"

nNebulaScriptClass(nguiwindowslider, "nguiwindow")

//----------------------------------------------------------------------------
/**
*/
nguiwindowslider::nguiwindowslider():
    nGuiWindow(),
    modal(false),
    dismissed(false),
    closeRequested(false),
    fadeInTime(0.0),
    fadeOutTime(0.0),
    openedTime(0.0),
    closeRequestTime(0.0),
    windowColor(1.0f, 1.0f, 1.0f, 1.0f),
    openFirstFrame(true),
		mouseOver(false),
		slideRate(0.001f)

{
    // windows are by default hidden
    this->windowClass = kernelServer->FindClass("nguiwindowslider");
    n_assert(this->windowClass);
}

//----------------------------------------------------------------------------
/**
*/
nguiwindowslider::~nguiwindowslider()
{
}

bool
nguiwindowslider::OnMouseMoved(const vector2& mousePos)
{
    if (this->Inside(mousePos))
    {
        this->mouseOver = true;
    }
    else
    {
        this->mouseOver = false;
    }
    return nGuiWidget::OnMouseMoved(mousePos);
}


bool
nguiwindowslider::Render()
{
	if (nGuiWidget::IsShown())
    {
        nGuiServer* guiServer = nGuiServer::Instance();

				this->nGuiWindow::UpdateWindowColor();
        vector4 globalColor = guiServer->GetGlobalColor();
        guiServer->SetGlobalColor(this->windowColor);
				guiServer->DrawBrush(nGuiWindow::GetScreenSpaceRect(), nGuiWidget::defaultBrush);

        // take opened time stamp AFTER first rendering to take resource
        // loading delays into account
        if (this->openFirstFrame)
        {
            // NOTE: the use on nTimeServer is intentional, as the current
            // frame time stamp is useless (would give fade delays when
            // resources are loaded)
						this->rect.set(this->hideRect.v0,this->showRect.v1);
            this->openedTime = nTimeServer::Instance()->GetTime();
            this->openFirstFrame = false;
        }

        // render contained widgets
        nGuiWidget::Render();

				if (time == 0) time = nTimeServer::Instance()->GetTime();

				double delta = nTimeServer::Instance()->GetTime();

				slideRate = (float)(((delta - time))*0.1);

				time = delta;

        // restore previous global color
        guiServer->SetGlobalColor(globalColor);
				if(this->mouseOver)
				{
					vector2 v0(this->GetRect().v0);
					vector2 v1(this->GetRect().v1);

					if (v0.x > this->showRect.v0.x)
					{
						if ((v0.x - this->slideRate)>this->showRect.v0.x)
							v0.x -= this->slideRate;
						else
							v0.x = showRect.v0.x;
					}

					if (v0.y > this->showRect.v0.y)
					{
						if ((v0.y - this->slideRate)>this->showRect.v0.y)
							v0.y -= this->slideRate;
						else
							v0.y = showRect.v0.y;
					}

					if (v1.x > this->showRect.v1.x)
					{
						if ((v1.x - this->slideRate)>this->showRect.v1.x)
							v1.x -= this->slideRate;
						else
							v1.x = showRect.v1.x;
					}

					if (v1.y > this->showRect.v1.y)
					{
						if ((v1.y - this->slideRate)>this->showRect.v1.y)
							v1.y -= this->slideRate;
						else
							v1.y = showRect.v1.y;
					}

					if (v0.x < this->showRect.v0.x)
					{
						if ((v0.x + this->slideRate)<this->showRect.v0.x)
							v0.x += this->slideRate;
						else
							v0.x = showRect.v0.x;
					}

					if (v0.y < this->showRect.v0.y)
					{
						if ((this->slideRate+v0.y)<this->showRect.v0.y)
							v0.y += this->slideRate;
						else
							v0.y = showRect.v0.y;
					}

					if (v1.x < this->showRect.v1.x)
					{
						if ((this->slideRate+v1.x)<this->showRect.v1.x)
							v1.x += this->slideRate;
						else
							v1.x = showRect.v1.x;
					}

					if (v1.y < this->showRect.v1.y)
					{
						if ((this->slideRate+v1.y)<this->showRect.v1.y)
							v1.y += this->slideRate;
						else
							v1.y = showRect.v1.y;
					}

					rectangle auxRectangle(v0,v1);
//					this->nGuiWidget::SetRect(*(new rectangle(v0,v1)));
					this->nGuiWidget::SetRect(auxRectangle);
				}
				else
				{
					vector2 v0(this->GetRect().v0);
					vector2 v1(this->GetRect().v1);

					if (v0.x > this->hideRect.v0.x)
					{
						if ((v0.x - this->slideRate)>this->hideRect.v0.x)
							v0.x -= this->slideRate;
						else
							v0.x = hideRect.v0.x;
					}

					if (v0.y > this->hideRect.v0.y)
					{
						if ((v0.y - this->slideRate)>this->hideRect.v0.y)
							v0.y -= this->slideRate;
						else
							v0.y = hideRect.v0.y;
					}

					if (v1.x > this->hideRect.v1.x)
					{
						if ((v1.x - this->slideRate)>this->hideRect.v1.x)
							v1.x -= this->slideRate;
						else
							v1.x = hideRect.v1.x;
					}

					if (v1.y > this->hideRect.v1.y)
					{
						if ((v1.y - this->slideRate)>this->hideRect.v1.y)
							v1.y -= this->slideRate;
						else
							v1.y = hideRect.v1.y;
					}

					if (v0.x < this->hideRect.v0.x)
					{
						if ((this->slideRate+v0.x)<this->hideRect.v0.x)
							v0.x += this->slideRate;
						else
							v0.x = hideRect.v0.x;
					}

					if (v0.y < this->hideRect.v0.y)
					{
						if ((this->slideRate+v0.y)<this->hideRect.v0.y)
							v0.y += this->slideRate;
						else
							v0.y = hideRect.v0.y;
					}

					if (v1.x < this->hideRect.v1.x)
					{
						if ((this->slideRate+v1.x)<this->hideRect.v1.x)
							v1.x += this->slideRate;
						else
							v1.x = hideRect.v1.x;
					}

					if (v1.y < this->hideRect.v1.y)
					{
						if ((this->slideRate+v1.y)<this->hideRect.v1.y)
							v1.y += this->slideRate;
						else
							v1.y = hideRect.v1.y;
					}

          rectangle auxRectangle(v0,v1);
//					this->nGuiWidget::SetRect(*(new rectangle(v0,v1)));
					this->nGuiWidget::SetRect(auxRectangle);
				}
        return true;
    }
    return false;
}
//----------------------------------------------------------------------------
// EOF
//----------------------------------------------------------------------------
